
// Saves options to localStorage.
function save_options() {
	saveSelection("close_timer");
	
	localStorage["process_limit"]  = $('#limit').val();
	
	// Update status to let user know options were saved.
	var status = document.getElementById("status");
	status.innerHTML = "Options Saved.";
	setTimeout(function() {
		status.innerHTML = "";
	}, 2500);
}

function saveSelection(selectId) {
	var selection = document.getElementById(selectId);
	localStorage[selectId] = selection.children[selection.selectedIndex].value;
}

function getSelection(selectId) {
	var storedVal = localStorage[selectId];
	if (!storedVal) {
		return;
	}
	
	var select = document.getElementById(selectId);
	for (var i = 0; i < select.children.length; i++) {
		var child = select.children[i];
		if (child.value == storedVal) {
			child.selected = "true";
			break;
		}
	}
}

// Restores select box state to saved value from localStorage.
function restore_options() {
	if(!getSelection("close_timer"))
		localStorage["close_timer"] = 12500;
	if(!localStorage["process_limit"])
		localStorage["process_limit"] = 0;
		
	$('#limit').val(localStorage["process_limit"]);
}

function testField(field) {
    var regExpr = new RegExp("^\d*\.?\d*$");
	if (!regExpr.test(field.value)) {
	  // Case of error
	  field.value = "";
	}
}

$(document).ready(function() {
	$('#close').bind('click', function() {
		window.close();
	});
	$('#back').bind('click', function() {
		location.replace('popup.html');
	});
	
	$('#limit').jStepper({minValue:0, maxValue:9999, defaultValue: 0, allowDecimals: false, decimalSeperator: ''});
	
	document.querySelector('#save').addEventListener('click', save_options);
	restore_options();
});

